<template>
	<view class="container">
		<view class="header-wrap">
			<text class="title">历史记录列表</text>
			<view class="filter" @click="onOpen()">
				<uni-icons type="settings" size="24"></uni-icons>
				<text>筛选</text>
			</view>
		</view>

		<view class="content-wrap">
			<view class="item" v-for="item in activityList">
				<text class="no">活动编号：{{ item.activityNo }}</text>
				<view class="line"></view>
				<text class="title">{{ item.activityName }}</text>
				<view class="number">
					<text class="total-num">总数量：{{ item.couponNum || 0 }} 张</text>
					<text>剩余数量： {{ (item.couponNum - item.couponGet) || 0 }} 张</text>
				</view>
				<view class="number">
					<text class="time">活动时间：{{ dayjs(item.startTime).format('YYYY.MM.DD') }}~{{ dayjs(item.endTime).format('YYYY.MM.DD') }}</text>
					<view class="info" @click="onDetail(item.activityId,(item.couponNum - item.couponGet) || 0)">查看详情</view>
				</view>
			</view>
			<gzListNull :list="activityList"></gzListNull>
		</view>

		<!-- 普通弹窗 -->
		<uni-popup ref="searchPopup" type="top">
			<view class="popup-content">
				<view class="item">
					<text>活动编号</text>
					<input class="input" placeholder-style="color:#BBBBBB" placeholder="请输入活动编号" v-model="vdata.queryParams.activityNo" />
				</view>
				<view class="item">
					<text>活动名称</text>
					<input class="input" placeholder-style="color:#BBBBBB" placeholder="请输入活动名称" v-model="vdata.queryParams.activityName" />
				</view>
				<view class="item">
					<text>活动时间</text>
					<view class="time-wrap">
						<view class="time" @click="onOpenDate(true)">
							<text :style="vdata.queryParams.startTime?'':'color:#BBBBBB'">{{vdata.queryParams.startTime?vdata.queryParams.startTime:'开始时间'}}</text>
							<uni-icons type="down" color="#A0A9C0" size="20"></uni-icons>
						</view>
						<text class="to">至</text>
						<view class="time" @click="onOpenDate(false)">
							<text :style="vdata.queryParams.endTime?'':'color:#BBBBBB'">{{vdata.queryParams.endTime?vdata.queryParams.endTime:'结束时间'}}</text>
							<uni-icons type="down" color="#A0A9C0" size="20"></uni-icons>
						</view>
					</view>
				</view>
				<view class="bottom-wrap">
					<text class="reset" @click="onReset">重置</text>
					<text class="query" @click="onQuery()">查询</text>
				</view>
			</view>
		</uni-popup>

		<!-- 日历 -->
		<uni-calendar ref="calendarRef" :insert="false" @confirm="onConfirmDate" />
	</view>
</template>

<script setup>
	import dayjs from 'dayjs'
	import { reactive, ref, } from 'vue'
	import { $couponHistoryList } from '@/http/marketing/apiMarketing.js'
	import useGetList from '@/hooks/useGetList.js'
	import storageManage from '@/util/storageManage.js'
	import {onLoad,onShow} from '@dcloudio/uni-app'

	// const { useDataResult, getList } = useGetList($couponHistoryList, data => data, { agentNo: storageManage.userInfo().khjlAgentNo })

	// 搜索弹窗
	const searchPopup = ref();

	// 搜索弹窗
	const calendarRef = ref();

	const vdata = reactive({
		queryParams: {
			activityNo: '',
			activityName: '',
			startTime: '',
			endTime: ''
		},
		isStartDate: true,
	})
	
	// 获取列表数据
	const activityList = ref([])
	
	onLoad(()=>{
		$couponHistoryList({ agentNo: storageManage.userInfo().khjlAgentNo }).then(res=>{
			// console.log(res.bizData)
			activityList.value = res.bizData
		})
	})
	

	/**
	 * 打开弹窗
	 */
	const onOpen = () => {
		getApp().preventActive(()=>{
			searchPopup.value.open();
		})
		
	}
	/**
	 * 筛选
	 */
	const onQuery = () => {
		getList(vdata.queryParams)
		onClose()
	}
	/**
	 * 重置
	 */
	const onReset = () => {
		vdata.queryParams = {
			activityNo: '',
			activityName: '',
			startTime: '',
			endTime: ''
		}
	}

	/**
	 * 关闭弹窗
	 */
	const onClose = () => {
		searchPopup.value.close();
	}

	/**
	 * 打开日历
	 */
	const onOpenDate = (isStartDate) => {
		vdata.isStartDate = isStartDate
		calendarRef.value.open();
	}

	/**
	 * 日期选择
	 */
	const onConfirmDate = (result) => {
		if (vdata.isStartDate) {
			vdata.queryParams.startTime = result.fulldate
		} else {
			vdata.queryParams.endTime = result.fulldate
		}

	}

	/**
	 *  详情
	 */
	const onDetail = (activityId, remainNum) => {
		getApp().preventActive(()=>{
			uni.navigateTo({
				url: '/marketing/coupon/detail?activityId=' + activityId + '&remainNum=' + remainNum
			})
		})
		
	}
</script>

<style lang="less">
	@page-backgroud: #F6F7FC; //页面背景色

	page {
		background: @page-backgroud;
	}

	.container {
		min-height: calc(100vh - 60rpx);
		margin: 30rpx 20rpx 30rpx 20rpx;
		overflow: hidden;

		.header-wrap {
			width: 100%;
			position: fixed;
			height: 120rpx;
			top: 0;
			left: 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			z-index: 2;
			box-sizing: border-box;
			padding: 0 30rpx;
			background-color: @page-backgroud;

			.title {
				font-weight: bold;
				font-size: 32rpx;
				color: #333333;
				font-style: normal;
				flex-grow: 1;
			}


			.filter {
				display: flex;
				align-items: center;
				border-radius: 40rpx;
				background-color: white;
				padding: 8rpx 20rpx;
				border: 1rpx solid #C8C8C8;

				text {
					font-size: 26rpx;
					color: black;
					letter-spacing: 1px;
					margin-bottom: 2rpx;
				}
			}

		}

		.content-wrap {
			width: 100%;
			margin-top: 100rpx;
			position: relative;
			z-index: 0;

			.item {
				box-sizing: border-box;
				padding: 30rpx;
				background: #fff;
				border-radius: 20rpx;
				box-shadow: 0rpx -10rpx 30rpx -30rpx rgba(0, 0, 0, 0.8);
				display: flex;
				flex-direction: column;
				margin-bottom: 30rpx;


				.no {
					font-size: 26rpx;
					color: #333333;
				}

				.line {
					height: 1rpx;
					background-color: #E6E6E6;
					margin: 20rpx 0rpx;
				}

				.title {
					font-weight: bold;
					font-size: 32rpx;
					color: #333333;
					margin: 15rpx 0rpx;
				}

				.number {
					display: flex;
					align-items: center;
					color: darkgray;
					font-size: 26rpx;
					margin: 5rpx 0rpx;

					.total-num {
						margin-right: 50rpx;
					}

					.time {
						flex-grow: 1;
					}

					.info {
						border: 1rpx solid #C8C8C8;
						border-radius: 35rpx;
						background-color: white;
						padding: 15rpx 30rpx;
						color: #333333;
						box-shadow: 0rpx -10rpx 30rpx -30rpx rgba(0, 0, 0, 0.8);
					}
				}

			}
		}

		.popup-content {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 40rpx 30rpx;
			font-size: 26rpx;
			background-color: white;
			border-radius: 0 0 20rpx 20rpx;
			letter-spacing: 1px;
			box-sizing: border-box;
			margin-top: 120rpx;

			.item {
				display: flex;
				flex-direction: column;
				font-size: 28rpx;
				margin-bottom: 30rpx;
				width: 100%;

				.input {
					border-radius: 40rpx;
					padding: 18rpx 30rpx;
					background-color: #F6F7FC;
					margin-top: 20rpx;
					font-size: 30rpx;
				}

				.time-wrap {
					display: flex;
					flex-direction: row;
					align-items: center;
					margin-top: 20rpx;

					.time {
						border-radius: 40rpx;
						padding: 18rpx 30rpx;
						background-color: #F6F7FC;
						font-size: 30rpx;
						flex-grow: 1;
						display: flex;
						justify-content: space-between;
					}

					.to {
						margin: 0 20rpx;
					}
				}

			}


			.bottom-wrap {
				display: flex;
				flex-direction: row;
				align-items: center;
				font-size: 30rpx;
				margin-top: 30rpx;
				margin-bottom: 10rpx;
				width: 100%;

				.reset {
					color: black;
					border-radius: 40rpx;
					width: 25%;
					padding: 18rpx 0rpx;
					background-color: #F4F5F5;
					text-align: center;
					margin-right: 30rpx;
				}

				.query {
					color: white;
					border-radius: 40rpx;
					width: 75%;
					padding: 18rpx 0rpx;
					background-color: #24C789;
					margin-top: 30rpx;
					text-align: center;
					margin-bottom: 10rpx;
				}
			}

		}

	}
</style>